/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.web.manager.product;

import java.util.Date;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hutech09.jsale.bsn.IProductManager;
import org.hutech09.jsale.bsn.ISupplierManager;
import org.hutech09.jsale.entity.Product;
import org.hutech09.jsale.web.manager.model.LazySupplierDataModel;

/**
 *
 * @author Nam
 */
@Stateless
@ManagedBean
@SessionScoped
public class ProductCreateCtrl {

    @EJB
    private IProductManager productMgr;

    @EJB
    private ISupplierManager supplierManager;

    @ManagedProperty("#{productBrowseCtrl}")
    private ProductBrowseCtrl productBrowse;

    private Product product = new Product();

    private LazySupplierDataModel dataModel;

    @PostConstruct
    public void init() {
        dataModel = new LazySupplierDataModel(supplierManager);
    }

    
    public String create() {
        System.err.println(product);
        if (product.getSupplier() == null) {
            {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select an supplier!", ""));
            }
            return "";
        }
        product.setCreatedDate(new Date());
        Product saved = productMgr.create(product);
        productBrowse.setSelected(saved);
        product = new Product();
        return "productPage";
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public LazySupplierDataModel getDataModel() {
        return dataModel;
    }

    public void setDataModel(LazySupplierDataModel dataModel) {
        this.dataModel = dataModel;
    }

    public void setProductBrowse(ProductBrowseCtrl productBrowse) {
        this.productBrowse = productBrowse;
    }

}
